package com.ht.service.impl;

import com.ht.data.Result;
import com.ht.domain.bo.ArticleBO;
import com.ht.domain.po.*;
import com.ht.mapper.*;
import com.ht.service.ArticleService;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 科普文章service
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private HospitalMapper hospitalMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private PopularizationMapper popularizationMapper;

    @Autowired
    private LevelMapper levelMapper;

    @Autowired
    private DoctorMapper doctorMapper;


    /**
     * 根据科普文章id修改科普文章状态
     * @param articleId 文章id
     * @param articleStatus 状态
     * @return
     */
    @Override
    public Result updateArticleStatusByArticleId(String articleId, String articleStatus) {
        Article article = mongoTemplate.findById(articleId, Article.class);
        if (article.getArticleStatus().equals(articleStatus)){
            return Result.OK();
        }
        Query query = new Query(Criteria.where("_id").is(articleId));
        Update update = new Update();
        update.set("article_status",articleStatus);
        UpdateResult upsert = mongoTemplate.upsert(query, update, Article.class);
        //判断是否修改成功
        if (upsert.getModifiedCount() <= 0){
            return Result.ERROR();
        }
        return Result.OK();
    }

    /**
     * 查询所有文章
     * @return
     */
    @Override
    public List<ArticleBO> findArticleAll() {
        List<Article> articleList = mongoTemplate.findAll(Article.class);
        List<ArticleBO> articleBOList = new ArrayList<>();
        for (Article article:articleList) {
            TbHospital hospital = hospitalMapper.selectById(Long.valueOf(article.getHospital()));
            TbDepartment department = departmentMapper.selectById(Long.valueOf(article.getDepartment()));
            TbPopularization popularization = popularizationMapper.selectById(Long.valueOf(article.getPopularizationId()));
            TbLevel level = levelMapper.selectById(Long.valueOf(article.getLevel()));
            TbDoctor doctor = doctorMapper.selectById(Long.valueOf(article.getDoctor()));
            ArticleBO articleBO = new ArticleBO();
            BeanUtils.copyProperties(article,articleBO);
            //医院名称
            articleBO.setHospitalName(hospital.getHospitalName());
            //科室名称
            articleBO.setDepartmentName(department.getDepartmentName());
            //科普号名称
            articleBO.setPopularizationName(popularization.getPopularizationName());
            //医生名称
            articleBO.setDoctorName(doctor.getDoctorName());
            //职位名称
            articleBO.setLevelName(level.getLevel());
            articleBOList.add(articleBO);
        }
        return articleBOList;
    }

    /**
     * 按照类型查询
     * @param typeId 类型
     * @return
     */
    @Override
    public List<ArticleBO> findArticleByTypeId(String typeId) {
        List<Article> articleList = mongoTemplate.find(new Query(Criteria.where("article_type").is(typeId)), Article.class);
        List<ArticleBO> articleBOList = new ArrayList<>();
        for (Article article:articleList) {
            TbHospital hospital = hospitalMapper.selectById(Long.valueOf(article.getHospital()));
            TbDepartment department = departmentMapper.selectById(Long.valueOf(article.getDepartment()));
            TbPopularization popularization = popularizationMapper.selectById(Long.valueOf(article.getPopularizationId()));
            TbLevel level = levelMapper.selectById(Long.valueOf(article.getLevel()));
            TbDoctor doctor = doctorMapper.selectById(Long.valueOf(article.getDoctor()));
            ArticleBO articleBO = new ArticleBO();
            BeanUtils.copyProperties(article,articleBO);
            //医生名称
            articleBO.setHospitalName(hospital.getHospitalName());
            //科室名称
            articleBO.setDepartmentName(department.getDepartmentName());
            //科普号名称
            articleBO.setPopularizationName(popularization.getPopularizationName());
            //医生名称
            articleBO.setDoctorName(doctor.getDoctorName());
            //职位名称
            articleBO.setLevelName(level.getLevel());
            articleBOList.add(articleBO);
        }
        return articleBOList;
    }
}
